﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Text;
using System.Threading;

public partial class TrackingManage_TrackingDtl : BasePage
{
    IBNTrackingHandler trackingManager = new BNTrackingHandler();
    private static bool isDelete = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        string backUrl = Request["rtn"].ToString();
        if (backUrl == null || backUrl == "" || backUrl == "-1")
        {
            lblReturn.HRef = "javascript:history.go(-1);";
        }
        else
        {
            lblReturn.HRef = backUrl;
        }

        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();

                if (!strPermission.Contains("1066"))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
                else
                {
                    if (strPermission.Contains("1067"))
                    {
                        lblTrackingAdd.Visible = true;
                    }
                    else
                    {
                        lblTrackingAdd.Visible = false;
                    }
                    if (strPermission.Contains("1068"))
                    {
                        isDelete = true;
                    }
                    else
                    {
                        isDelete = false;
                    }
                }

            }
           
            dataBind();
        }

        lnkAdd.HRef = "TrackingAdd.aspx?STID=" + Request["STID"].ToString() + "&TID=" + Request["TID"].ToString() + "&rtn=" + Request["rtn"].ToString();
    }

    private void dataBind()
    {
        string sqlStr = @"SELECT contractor.CNShortName as Contractor, contractor.HKShortName as HKContractor, contractor.ENGShortName as ENGContractor
                                , truck.LicenseNO, driver.Name as Driver
                          FROM BN_Truck truck LEFT JOIN
                            Sy_Client contractor ON truck.ClientID = contractor.PKID INNER JOIN
                            BN_SendTruckDtl stDtl ON truck.PKID = stDtl.TruckID INNER JOIN
                            BN_Driver driver ON stDtl.Driver = driver.PKID
                          WHERE stDtl.SendTruckID = @SendTruckID AND
                                truck.PKID = @TruckID";
        SqlParameter[] parms = new SqlParameter[2];
        parms[0] = new SqlParameter("@SendTruckID", Request["STID"].ToString());
        parms[1] = new SqlParameter("@TruckID", Request["TID"].ToString());
        DataTable dt = trackingManager.GetList(sqlStr, parms);

        if (dt != null && dt.Rows.Count > 0)
        {
            DataRow dr = dt.Rows[0];
            string contractor = string.Empty;
            if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("en"))
            {
                contractor = dr["ENGContractor"].ToString();
            }
            else if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("zh-hk"))
            {
                contractor = dr["HKContractor"].ToString();
            }
            else
            {
                contractor = dr["Contractor"].ToString();
            }
            lblContractor.Text = contractor;
            lblDriver.Text = dr["Driver"].ToString();
            lblLicenseNO.Text = dr["LicenseNO"].ToString();
         }

         dataBindDtl();
    }

    private void dataBindDtl()
    {
        string sqlStr = @"SELECT truckstatus.StatusName as TruckStatus, truckstatus.HKStatusName as HKTruckStatus, truckstatus.ENGStatusName as ENGTruckStatus,
                            tracking.CBDate, tracking.CEDate, u.UserName as CreateUser,
                            tracking.CreatedDate, tracking.Remark, tracking.PKID
                          FROM BN_TTracking tracking LEFT JOIN
                            Sy_TruckStatus truckstatus ON tracking.TruckStatusID = truckstatus.PKID LEFT JOIN
                            Users u ON tracking.CreatedUser = u.UserID
                          WHERE tracking.TruckID = @TruckID AND
                                tracking.SendTruckID = @SendTruckID
                          ORDER BY truckstatus.Sequence asc";
        SqlParameter[] parms = new SqlParameter[2];
        parms[0] = new SqlParameter("@SendTruckID", Request["STID"].ToString());
        parms[1] = new SqlParameter("@TruckID", Request["TID"].ToString());

        DataTable dt = trackingManager.GetList(sqlStr, parms);
        DataView dv = dt.DefaultView;
        gvTrackingList.DataSource = dv;
        gvTrackingList.DataBind();
    }

    protected void gvTrackingList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btnDel = (LinkButton)e.Row.FindControl("lbtnDel");

            if (isDelete )
            {
                btnDel.Visible = true;
            }
            else
            {
                btnDel.Visible = false;
            }

            string language = Thread.CurrentThread.CurrentUICulture.ToString().ToLower();
            Label lblTruckStatus = (Label)e.Row.FindControl("lblTruckStatus");
            Label lblHKTruckStatus = (Label)e.Row.FindControl("lblHKTruckStatus");
            Label lblENGTruckStatus = (Label)e.Row.FindControl("lblENGTruckStatus");

            if ("en".Equals(language))
            {
                lblTruckStatus.Visible = false;
                lblHKTruckStatus.Visible = false;
                lblENGTruckStatus.Visible = true;
            }
            else if ("zh-hk".Equals(language))
            {
                lblTruckStatus.Visible = false;
                lblHKTruckStatus.Visible = true;
                lblENGTruckStatus.Visible = false;
            }
            else
            {
                lblTruckStatus.Visible = true;
                lblHKTruckStatus.Visible = false;
                lblENGTruckStatus.Visible = false;
            }
        }
    }

    protected void gvTrackingList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            string pkid = e.CommandArgument.ToString();
            string sqlStr = "SELECT * FROM BN_TTracking WHERE PKID > @PKID AND TruckID=@TruckID AND SendTruckID=@SendTruckID";
            SqlParameter[] parms = new SqlParameter[3];
            parms[0] = new SqlParameter("@PKID", pkid);
            parms[1] = new SqlParameter("@TruckID", Request["TID"].ToString());
            parms[2] = new SqlParameter("@SendTruckID", Request["STID"].ToString());
            DataTable dt = trackingManager.GetList(sqlStr, parms);

            if (dt != null && dt.Rows.Count > 0)
            {
                //Jscript.AlertInAjax("不能删除中间动态!", UpdatePanel1);
                Jscript.AlertInAjax(Resources.Messages.DeleteFailed, UpdatePanel1);
                return;
            }

            if (trackingManager.DeleteByID(int.Parse(pkid)))
            {
                Jscript.AlertInAjax(Resources.Messages.DeleteSuccess, UpdatePanel1);
            }
            else
            {
                Jscript.AlertInAjax(Resources.Messages.DeleteFailed, UpdatePanel1);
            }
        }
        dataBind();

    }

}
